//
//  LYLoginViewController.m
//  artist
//
//  Created by Jay_Apple on 16/3/8.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYLoginViewController.h"
#import "LYRegisterViewController.h"
#import "LYLoginInfoModel.h"
#import "LYUserInfoModel.h"

@interface LYLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userAccount;

@property (weak, nonatomic) IBOutlet UITextField *pwd;

@property (weak, nonatomic) IBOutlet UIButton *regisBtn;

@end

@implementation LYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.regisBtn.layer.borderColor = [UIColor colorWithHexString:@"#FF8400" alpha:1.0].CGColor;
    self.regisBtn.layer.borderWidth = 1;
    
    [self setUpNav];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 初始化
- (void)setUpNav{

    self.navigationItem.title = @"登录";
    //取消按钮
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(dismiss)];
    self.navigationItem.rightBarButtonItem = item;

}

#pragma mark - 按钮点击事件

/**
 *  登录
 */
- (IBAction)loginBtnOnclick:(id)sender {
    
    if (!([self checkUser] && [self checkpwd])) {
        return;
    }
    
    [MBProgressHUD showMessage:@"正在登录"];
    //网络请求
    WS(weakSelf)
    [LYRequestTool loginWithUserName:self.userAccount.text Password:self.pwd.text success:^(LYLoginInfoModel *info) {
        
        [AppDelegate shareInstance].token = info.token;
        //存到本地
        [VVYSaveDataManager saveDataWithData:info key:LYUserLoginInfo];
        
        //拿到token请求用户信息
        [LYRequestTool acquireUserInfoWithToken:info.token success:^(LYUserInfoModel *userInfo) {
            info.pwd = weakSelf.pwd.text;
            [LYPublicMethod  loginRongLianYunWithLoginInfo:userInfo];
            [MBProgressHUD hideHUD];
            [MBProgressHUD showSuccess:@"登录成功！"];

            //保存本地
            [VVYSaveDataManager saveDataWithData:userInfo key:KEY_USERMESSAGE];
            [[VVYCheckStatus sharedInstance] checkIsLogin];

            if (self.block) {
                self.block(userInfo);
            }
            
            [self dismiss];
            
        } failure:^(NSString *code, NSString *message) {
            
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:message];
            
        }];
        
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:message];
    }];
}



/**
 *  注册
 */
- (IBAction)reigsterBtnOnclick:(id)sender {
    
    LYRegisterViewController *regVC = [[LYRegisterViewController alloc] init];
    regVC.type = viewTypeRegister;
    [self.navigationController pushViewController:regVC animated:YES];
    
}

/**
 *  忘记密码
 */
- (IBAction)forgetPwd:(id)sender {
    
    LYRegisterViewController *regVC = [[LYRegisterViewController alloc] init];
    regVC.type = viewTypePwd;
    [self.navigationController pushViewController:regVC animated:YES];
}


//取消
- (void)dismiss{

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}

#pragma mark - 文本校验
- (BOOL)checkUser{

    if (self.userAccount.text.length > 0) {
        return YES;
    }else{
    
        [MBProgressHUD showError:@"账号不能为空"];
        return NO;
    }

}

- (BOOL)checkpwd{


    if (self.pwd.text.length > 0) {
        return YES;
    }else{
    
        [MBProgressHUD showError:@"密码不能为空"];
        return NO;
    }
}



@end
